package frame.options.virtual.create;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import model.components.networks.virtual.VirtualNetworks;

import com.nexes.wizard.WizardPanelDescriptor;


public class CreateVirtualSelectDescriptor extends WizardPanelDescriptor implements DocumentListener, ActionListener
{
	public static final String IDENTIFIER = CreateVirtualSelectDescriptor.class.getSimpleName();
    
	private CreateVirtualSelectPanel createVirtualSelectPanel;
	public CreateVirtualSelectPanel getCreateVirtualSelectPanel(){ return createVirtualSelectPanel; }
	public void setCreateVirtualSelectPanel( CreateVirtualSelectPanel createVirtualSelectPanel ){ this.createVirtualSelectPanel = createVirtualSelectPanel; }
	
	private CreateVirtualWizard createVirtualWizard;
	public CreateVirtualWizard getCreateVirtualWizard(){ return createVirtualWizard; }
	public void setCreateVirtualWizard( CreateVirtualWizard createVirtualWizard ){ this.createVirtualWizard = createVirtualWizard; }
	
	public CreateVirtualSelectDescriptor()
    {
    	this.setCreateVirtualSelectPanel( new CreateVirtualSelectPanel() );
    	this.getCreateVirtualSelectPanel().addListeners( this, this );
        
    	this.setPanelDescriptorIdentifier( IDENTIFIER );
        this.setPanelComponent( this.getCreateVirtualSelectPanel() );
    }
    
	@Override
	public Object getBackPanelDescriptor()
    {
        return null;
    }
	@Override
	public Object getNextPanelDescriptor()
    {
        return CreateVirtualFinishDescriptor.IDENTIFIER;
    }
	@Override
	public void aboutToDisplayPanel()
    {
    	this.setCreateVirtualWizard( (CreateVirtualWizard) this.getWizard() );
    	this.getCreateVirtualWizard().setNextFinishButtonEnabled( false );
    	this.enableFinishButton();
    }
	public void aboutToHidePanel()
	{
		this.createVirtualNetworks();
	}
	private void createVirtualNetworks()
	{
		CreateVirtualSelectPanel createVirtualSelectPanel = this.getCreateVirtualSelectPanel();
				
		int networksNumber = Integer.parseInt( createVirtualSelectPanel.getNetworksNumberTextField().getText() );
		int windowTimeUnitsNumber = Integer.parseInt( createVirtualSelectPanel.getWindowTimeUnitsNumberTextField().getText() );
		double poissonArrives = Double.parseDouble( createVirtualSelectPanel.getPoissonArrivesTextField().getText() );
		double exponentialLifetime = Double.parseDouble( createVirtualSelectPanel.getExponentialLifetimeTextField().getText() );
		
		int minRoutersNumber = Integer.parseInt( createVirtualSelectPanel.getMinRoutersNumberTextField().getText() );
    	int maxRoutersNumber = Integer.parseInt( createVirtualSelectPanel.getMaxRoutersNumberTextField().getText() );

		int minSwitchesNumber = Integer.parseInt( createVirtualSelectPanel.getMinSwitchesNumberTextField().getText() );
    	int maxSwitchesNumber = Integer.parseInt( createVirtualSelectPanel.getMaxSwitchesNumberTextField().getText() );
		
    	int minServersNumber = Integer.parseInt( createVirtualSelectPanel.getMinServersNumberTextField().getText() );
    	int maxServersNumber = Integer.parseInt( createVirtualSelectPanel.getMaxServersNumberTextField().getText() );
    	
    	int linuxPercentage = Integer.parseInt( createVirtualSelectPanel.getLinuxPercentageTextField().getText() );
		int windowsPercentage = Integer.parseInt( createVirtualSelectPanel.getWindowsPercentageTextField().getText() );
		int solarisPercentage = Integer.parseInt( createVirtualSelectPanel.getSolarisPercentageTextField().getText() );
		int androidPercentage = Integer.parseInt( createVirtualSelectPanel.getAndroidPercentageTextField().getText() );
		
		int minCPU = Integer.parseInt( createVirtualSelectPanel.getMinCPUtextField().getText() );
		int maxCPU = Integer.parseInt( createVirtualSelectPanel.getMaxCPUtextField().getText() );
		
		int minRAM = Integer.parseInt( createVirtualSelectPanel.getMinRAMtextField().getText() );
		int maxRAM = Integer.parseInt( createVirtualSelectPanel.getMaxRAMtextField().getText() );
		
		int minStorage = Integer.parseInt( createVirtualSelectPanel.getMinStorageTextField().getText() );
		int maxStorage = Integer.parseInt( createVirtualSelectPanel.getMaxStorageTextField().getText() );
		
		int minBW = Integer.parseInt( createVirtualSelectPanel.getMinBWtextField().getText() );
		int maxBW = Integer.parseInt( createVirtualSelectPanel.getMaxBWtextField().getText() );
		
		VirtualNetworks virtualNetworks = new VirtualNetworks( VirtualNetworks.NAME_PREFIX, networksNumber, windowTimeUnitsNumber, poissonArrives, exponentialLifetime, minRoutersNumber, maxRoutersNumber, minSwitchesNumber, maxSwitchesNumber, minServersNumber, maxServersNumber, linuxPercentage, windowsPercentage, solarisPercentage, androidPercentage, minCPU, maxCPU, minRAM, maxRAM, minStorage, maxStorage, minBW, maxBW );
		this.getCreateVirtualWizard().setVirtualNetworks( virtualNetworks.getVirtualNetworks() );
	}
    
	@Override
	public void actionPerformed( ActionEvent actionEvent )
	{
		this.enableFinishButton();
	}
	@Override
    public void changedUpdate( DocumentEvent documentEvent )
    {
    	this.enableFinishButton();
    }
	@Override
	public void insertUpdate( DocumentEvent documentEvent )
	{
		this.enableFinishButton();
	}
	@Override
	public void removeUpdate( DocumentEvent documentEvent )
	{
		this.enableFinishButton();
	}
	
	private void enableFinishButton()
	{
		if( this.getCreateVirtualSelectPanel().check() )
			this.getCreateVirtualWizard().setNextFinishButtonEnabled( true );
		else
        	this.getCreateVirtualWizard().setNextFinishButtonEnabled( false );
    }
	
}
